package com.example.app;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.aidl.ICatService;
import com.example.aidl.bean.Cat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AIDLActivity extends AppCompatActivity {
    public static final String TAG = "AIDLActivity";
    private ICatService iCatService;
    private TextView tv;
    private boolean conncted;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

//        Messenger
//        ContentResolver

        tv = findViewById(R.id.tv);
        Intent intent = new Intent();
        intent.putExtra("a", "a1");
        Bundle b = new Bundle();
        b.putInt("b", 10);
        intent.putExtra("b", b);
        intent.setComponent(new ComponentName("com.example.server", "com.example.server.AIDLService"));//显示意图
        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
    }

    private void setTv() {
        try {
            Cat cat = iCatService.getCat();
            int catCount = iCatService.getCatCount();
            tv.setText(cat.name+"_"+cat.price+"_"+catCount);//tom_100_10000

            //<in,把对象传入服务端，服务端即使修改了值，客户端也不会发生改变>
            Cat t1 = new Cat("t1", 1);
            iCatService.addCatIn(t1);
            Log.d(TAG, "In-t1->"+t1.toString()+"/"+t1.hashCode());//In-t1->Cat{name='t1', age=0, price=1}/204095942

            //<out,把对象传入服务端，服务端修改之后，对象的值被修改了>
            Cat t2 = new Cat("t2", 2);
            iCatService.addCatOut(t2);
            Log.d(TAG, "Out-t2->"+t2.toString()+"/"+t2.hashCode());//Out-t2->Cat{name='def', age=0, price=102}/53249415

            //<inout,上面两种情况的综合>
            Cat t3 = new Cat("t3", 3);
            iCatService.addCatInout(t3);
            Log.d(TAG, "Inout-t3->"+t3.toString()+"/"+t3.hashCode());//Inout-t3->Cat{name='hij', age=0, price=103}/135146676

            //<bundle,传输>
            Cat t4 = new Cat("t4", 4);
            Bundle bundle = new Bundle();
            bundle.putParcelable("cat", t4);
            iCatService.saveCat(bundle);

            List<Cat> cats = new ArrayList<>();
//            List<Cat> cats = new LinkedList<>();
            cats.add(new Cat("a", 44));
            cats.add(new Cat("b", 45));
            iCatService.saveListCat(cats);

            Map<String, Cat> catMap = new HashMap<>();
//            Map<String, Cat> catMap = new Hashtable<>();
            catMap.put("x", new Cat("x", 66));
            catMap.put("y", new Cat("y", 67));
            catMap.put("z", new Cat("z", 68));
            iCatService.saveMapCat(catMap);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            conncted = true;
            Log.d(TAG, "绑定成功");
            iCatService = ICatService.Stub.asInterface(service);
            setTv();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            conncted = false;
            Log.d(TAG, "绑定失败");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
